#include "tokenset.h"

/** 
 * This file is part of uhferret.
 *
 * Author::    Peter Lane
 * Copyright:: Copyright 2011, Peter Lane.
 * License::   GPLv3
 *
 * uhferret is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * uhferret is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with uhferret.  If not, see <http://www.gnu.org/licenses/>.
 */

Token::Token ()
	: _capacity (2), _top (0)
{
	_token = new char [_capacity];
}

Token::~Token ()
{
	delete[] _token;
}

void Token::Erase ()
{
	_top = 0;
}

void Token::AddChar (char c)
{
	if (_top == _capacity)
		Grow ();
	_token [_top] = c;
	++_top;
}

std::string Token::GetString () const
{
	return std::string (_token, _top);
}

int Token::GetLength () const
{
	return _top;
}

void Token::Grow ()
{
	char * newtoken = new char [2 * _capacity];
	for (int i = 0; i < _capacity; ++i)
		newtoken[i] = _token[i];
	_capacity = 2 * _capacity;
	delete _token;
	_token = newtoken;
}

// *** TokenSet

TokenSet::TokenSet ()
	: _nextindex (0)
{}

std::size_t TokenSet::GetIndexFor (std::string token)
{
	_tokens_it = _tokens.find (token);
	if (_tokens_it != _tokens.end())  // found it
		return _tokens_it->second;
	else // otherwise, make a new index
	{
		_tokens[token] = _nextindex;
		_strings[_nextindex] = token;
		_nextindex++;
		return _nextindex-1;
	}
}

std::string TokenSet::GetStringFor (std::size_t token)
{
	_strings_it = _strings.find (token);
	assert (_strings_it != _strings.end ()); // it's an error if token not in token set
	return std::string (_strings_it->second.c_str ());
}

void TokenSet::Clear ()
{
	_tokens.clear ();
	_strings.clear ();
	_nextindex = 0;
}

void TokenSet::SetNextIndex (int index)
{
	_nextindex = index;
}

void TokenSet::SetIndexString (std::string token, int index)
{
	_strings[index] = token;
	_tokens[token] = index;
}
